import type { FlexSprayListType } from './flexSpray.vue'
import {
  DISHWASHER_OPTION_FLEXSPRAY_BACKLEFT,
  DISHWASHER_OPTION_FLEXSPRAY_BACKRIGHT,
  DISHWASHER_OPTION_FLEXSPRAY_FRONTLEFT,
  DISHWASHER_OPTION_FLEXSPRAY_FRONTRIGHT,
  DISHWASHER_ENUMTYPE_FLEXSPRAY_INTENSITY_DELICATE,
  DISHWASHER_ENUMTYPE_FLEXSPRAY_INTENSITY_NORMAL,
  DISHWASHER_ENUMTYPE_FLEXSPRAY_INTENSITY_HEAVY,
  DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_BACKLEFT,
  DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_BACKRIGHT,
  DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_FRONTLEFT,
  DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_FRONTRIGHT,
  DISHWASHER_OPTION_FLEXSPRAY,
  DISHWASHER_ENUMTYPE_FLEXSPRAY_TYPE_INDIVIDUAL,
} from '@/constants/Command'
import { selectProgram, type ApplianceDetailOption, type ApplianceSetting } from '@/api/appliance'
import type { DishwasherController } from '@/controller/Dishwasher/definition'
import type { WS } from '@/utils/socket/types'
import { cloneDeep } from 'lodash'

export function useFlexSpray(control: DishwasherController.Control) {
  /**
   * 精控强洗
   */
  const flexSprayEnumType = [
    {
      key: DISHWASHER_ENUMTYPE_FLEXSPRAY_INTENSITY_DELICATE,
      displayvalue: '轻柔',
    },
    {
      key: DISHWASHER_ENUMTYPE_FLEXSPRAY_INTENSITY_NORMAL,
      displayvalue: '标准',
    },
    {
      key: DISHWASHER_ENUMTYPE_FLEXSPRAY_INTENSITY_HEAVY,
      displayvalue: '强力',
    },
  ]

  const flexSprayList = ref<FlexSprayListType[]>([])
  const tempFlexSprayList = ref<FlexSprayListType[]>([])
  const customFlexSprayList = ref<FlexSprayListType[]>([])
  const tempCustomFlexSprayList = ref<FlexSprayListType[]>([])

  /**
   * 获取detail flexSprayList
   */
  async function getFlexSprayDataFun() {
    const options = control.store.detail?.option || []
    await options.forEach((item) => handleFlexSprayData(item))
    flexSprayList.value = tempFlexSprayList.value
  }
  /**
   * 处理detail中的option
   * @param option
   */
  function handleFlexSprayData(option: ApplianceDetailOption) {
    let index = 0
    let flag = false
    switch (option.key) {
      case DISHWASHER_OPTION_FLEXSPRAY_FRONTLEFT:
        index = 2
        flag = true
        break
      case DISHWASHER_OPTION_FLEXSPRAY_BACKLEFT:
        index = 0
        flag = true
        break
      case DISHWASHER_OPTION_FLEXSPRAY_BACKRIGHT:
        index = 1
        flag = true
        break
      case DISHWASHER_OPTION_FLEXSPRAY_FRONTRIGHT:
        index = 3
        flag = true
        break
      default:
        break
    }
    if (!flag) return
    tempFlexSprayList.value[index] = {
      key: option.key,
      value: option.value as string,
      displayvalue: option.displayvalue ? (option.displayvalue as string) : getDisplayvalue(option),
    }
  }
  /**
   * 获取setting flexSprayList
   */
  function getCustomSprayDataFun() {
    const settings = control.store._datasource.settings
    settings.forEach((item) => handleCustomModeSettingData(item))
    tempCustomFlexSprayList.value = cloneDeep(customFlexSprayList.value)
  }
  /**
   * 处理settings
   */
  function handleCustomModeSettingData(item: ApplianceSetting) {
    switch (item.key) {
      case DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_FRONTLEFT:
        customFlexSprayList.value[2] = {
          key: DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_FRONTLEFT,
          value: item.value as string,
          displayvalue: item.displayvalue ? (item.displayvalue as string) : getDisplayvalue(item),
        }
        break
      case DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_FRONTRIGHT:
        customFlexSprayList.value[3] = {
          key: DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_FRONTRIGHT,
          value: item.value as string,
          displayvalue: item.displayvalue ? (item.displayvalue as string) : getDisplayvalue(item),
        }
        break
      case DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_BACKLEFT:
        customFlexSprayList.value[0] = {
          key: DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_BACKLEFT,
          value: item.value as string,
          displayvalue: item.displayvalue ? (item.displayvalue as string) : getDisplayvalue(item),
        }
        break
      case DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_BACKRIGHT:
        customFlexSprayList.value[1] = {
          key: DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_BACKRIGHT,
          value: item.value as string,
          displayvalue: item.displayvalue ? (item.displayvalue as string) : getDisplayvalue(item),
        }
        break
      default:
        break
    }
  }
  /**
   * 获取displayvalue
   * @param settingItem
   */
  function getDisplayvalue(settingItem: ApplianceSetting) {
    let displayvalue = ''
    flexSprayEnumType.forEach((item) => {
      if (settingItem.value === item.key) {
        displayvalue = item.displayvalue
      }
    })
    return displayvalue
  }
  /**
   * 将custom数据作为individual数据
   */
  function transformCutomToIndividual() {
    flexSprayList.value?.forEach((item) => {
      tempCustomFlexSprayList.value?.forEach((customItem) => {
        if (item.key.indexOf('BackLeft') !== -1 && customItem.key.indexOf('BackLeft') !== -1) {
          item.value = getFlexSprayValue(customItem.displayvalue)
          item.displayvalue = customItem.displayvalue
        } else if (item.key.indexOf('BackRight') !== -1 && customItem.key.indexOf('BackRight') !== -1) {
          item.value = getFlexSprayValue(customItem.displayvalue)
          item.displayvalue = customItem.displayvalue
        } else if (item.key.indexOf('FrontLeft') !== -1 && customItem.key.indexOf('FrontLeft') !== -1) {
          item.value = getFlexSprayValue(customItem.displayvalue)
          item.displayvalue = customItem.displayvalue
        } else if (item.key.indexOf('FrontRight') !== -1 && customItem.key.indexOf('FrontRight') !== -1) {
          item.value = getFlexSprayValue(customItem.displayvalue)
          item.displayvalue = customItem.displayvalue
        }
      })
    })
  }
  /**
   * 获取value值
   * @param value
   * @returns
   */
  function getFlexSprayValue(value: string) {
    return flexSprayEnumType.find((item) => item.displayvalue === value)?.key || ''
  }
  /**
   * 监听
   */
  const observeSettingKeys = [
    DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_BACKLEFT,
    DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_BACKRIGHT,
    DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_FRONTLEFT,
    DISHWASHER_SETTING_FLEXSPRAY_CUSTOM_FRONTRIGHT,
  ]
  const observeOptionKeys = [
    DISHWASHER_OPTION_FLEXSPRAY_FRONTLEFT,
    DISHWASHER_OPTION_FLEXSPRAY_BACKLEFT,
    DISHWASHER_OPTION_FLEXSPRAY_BACKRIGHT,
    DISHWASHER_OPTION_FLEXSPRAY_FRONTRIGHT,
  ]
  function listenMessages() {
    control.remote.addBatchObserves([
      {
        matcher: (msg) => observeSettingKeys.includes(msg.key),
        handler: (msg) => handleFlexSprayCustomPosition(msg),
      },
      {
        matcher: (msg) => observeOptionKeys.includes(msg.key),
        handler: (msg) => handleFlexSprayPosition(msg),
      },
    ])
  }

  /**
   * 推送精控强洗常用模式各个方位
   */
  function handleFlexSprayCustomPosition(item: WS.MessageItem) {
    customFlexSprayList.value.forEach((listItem) => {
      if (listItem.key === item.key && listItem.value != item.value) {
        listItem.value = item.value as string
        if (item.displayvalue) {
          listItem.displayvalue = item.displayvalue
        } else {
          flexSprayEnumType.forEach((chooseItem) => {
            if (chooseItem.key == item.value) {
              listItem.displayvalue = chooseItem.displayvalue
            }
          })
        }
      }
    })
    tempCustomFlexSprayList.value = cloneDeep(customFlexSprayList.value)
  }

  /**
   * 推送精控强洗方位
   */
  function handleFlexSprayPosition(item: WS.MessageItem) {
    flexSprayList.value.forEach((listItem) => {
      if (listItem.key === item.key && listItem.value != item.value) {
        let value = item.value as string
        listItem.value = value
        if (item.displayvalue) {
          listItem.displayvalue = item.displayvalue
        } else {
          flexSprayEnumType.forEach((chooseItem) => {
            if (chooseItem.key == item.value) {
              listItem.displayvalue = chooseItem.displayvalue
            }
          })
        }
      }
    })
  }

  /**
   * 设置individual模式
   */
  async function selectedFlexSpray(haId: string, progromKey: string) {
    let options = [
      {
        key: DISHWASHER_OPTION_FLEXSPRAY,
        value: DISHWASHER_ENUMTYPE_FLEXSPRAY_TYPE_INDIVIDUAL,
      },
      {
        key: DISHWASHER_OPTION_FLEXSPRAY_FRONTLEFT,
        value: getFlexSprayNewValue(DISHWASHER_OPTION_FLEXSPRAY_FRONTLEFT),
      },
      {
        key: DISHWASHER_OPTION_FLEXSPRAY_BACKLEFT,
        value: getFlexSprayNewValue(DISHWASHER_OPTION_FLEXSPRAY_BACKLEFT),
      },
      {
        key: DISHWASHER_OPTION_FLEXSPRAY_BACKRIGHT,
        value: getFlexSprayNewValue(DISHWASHER_OPTION_FLEXSPRAY_BACKRIGHT),
      },
      {
        key: DISHWASHER_OPTION_FLEXSPRAY_FRONTRIGHT,
        value: getFlexSprayNewValue(DISHWASHER_OPTION_FLEXSPRAY_FRONTRIGHT),
      },
    ]
    await selectProgram(haId, progromKey, options)
  }

  function getFlexSprayNewValue(key: string) {
    return flexSprayList.value.find((item) => item.key === key)?.value as string
  }

  return {
    flexSprayList,
    customFlexSprayList,
    tempCustomFlexSprayList,
    getFlexSprayDataFun,
    getCustomSprayDataFun,
    transformCutomToIndividual,
    listenMessages,
    selectedFlexSpray,
  }
}
